﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PersonalAlbum.Data.Objects;

namespace PersonalAlbum.Data.Entities
{
    public class TagEntryRepository : ITagEntryRepository
    {
        private string _connectionString;
        public TagEntryRepository(string connectionString)
        {
            this._connectionString = connectionString;
        }
        #region ITagEntryRepository Members

        public List<PersonalAlbum.Data.Objects.Tag> GetTags()
        {
            using (var ImagesDB = new ImagesDal.SqlEntitiesProvider(this._connectionString))
            {
                return ImagesDB.Tag.Select(t => new PersonalAlbum.Data.Objects.Tag() { Name = t.Name, Id = t.Id, Count = t.Count }).ToList();
            }
        }

       

        public void DeleteTagName(string tagName)
        {
            using (var ImagesDB = new ImagesDal.SqlEntitiesProvider(this._connectionString))
            {
                var tags = ImagesDB.Tag.Where(s => s.Name == tagName);
                foreach (var item in tags)
                {
                    var relation = ImagesDB.TagEntryRelation.Where(s => s.TagId == item.Id);
                    foreach (var r in relation)
                    {
                        ImagesDB.DeleteObject(r);
                    }
                    ImagesDB.DeleteObject(item);
                }
                ImagesDB.SaveChanges();
            }
        }

        #endregion
    }
}
